/* Copyright (c) 2007 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. 
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRENTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 * $LastChangedRevision: 2290 $
 */ 
                        
/** @file
 *
 * Device and host common header file for the Wireless Desktop Protocol. 
 *
 * @author Lasse Olsen
 *
 * @defgroup nordic_protocol_wdp Wireless Desktop Protocol
 * @{
 */

#ifndef WDP_COMMON_H__
#define WDP_COMMON_H__

#include <stdint.h>
#include "wdp_setup.h"
#include "fap.h"

/** @name Host and device common application functions */
//@{

/**
Function for specifying the power mode in which the radio shall reside during 
IDLE mode. For full functional description, see fap_select_radio_idle_mode().

@sa fap_select_radio_idle_mode() 
*/
#define wdp_select_radio_idle_mode(a) fap_select_radio_idle_mode(a)

/**
Function for observing the current mode of the protocol.

@sa fap_get_mode()
*/
#define wdp_get_mode() fap_get_mode()

/** @} */

/**
Relation between device type and radio pipes.   
*/
typedef enum
{
  WDP_PAIRING_PIPE = FAP_DEVICE0,         // WDP SW requires this to equal 0
  WDP_MOUSE,                              // WDP SW requires this to equal 1
  WDP_KEYBOARD,                           // WDP SW requires this to equal 2
  WDP_REMOTE,                             // WDP SW requires this to equal 3
  WDP_DEV_TYPE_COUNT
} wdp_dev_types_t;

/**
WDP/FAP transparent definition. 
*/
#define WDP_PDOWN_IDLE FAP_PDOWN_IDLE

/**
WDP/FAP transparent definition. 
*/
#define WDP_STANDBY_IDLE FAP_STANDBY_IDLE 

/**
WDP/FAP transparent definition. 
*/
#define WDP_IDLE FAP_IDLE

/**
WDP/FAP transparent definition. 
*/
#define WDP_TRANSMITTING FAP_TRANSMITTING

/**
WDP/FAP transparent definition. 
*/
#define WDP_RECEIVING FAP_RECEIVING

/**
Wireless Desktop transmit package definition.
*/
#define WDP_TYPE_ID 0
#define WDP_APP_PL_START (WDP_TYPE_ID+1)

/**
Definition of possible package TYPE IDs in Wireless Desktop transmit package.
*/
typedef enum
{
  PAIRING_REQ,
  PAIRING_COMPL, 
  PAIRING_RESP,
  KEEP_ALIVE,
  USER_DATA
};

/**
Definition of pairing payload.
*/
#define DEV_TYPE WDP_APP_PL_START
#define ADDRESS_B0 (DEV_TYPE+1)
#define MASTER_ADDRESS_START (ADDRESS_B0+1)
#define WDP_PAIRING_PL_LENGTH (MASTER_ADDRESS_START + FAP_ADDRESS_WIDTH - 1)

/** 
Definition of payload length when sending a payload only containing the packet TYPE_ID.
**/
#define WDP_TYPE_ID_ONLY_PL 1     // xxx bort ?

/** 
Application user data payload suggestion.
*/
#define APP_CMD 0
#define APP_ATTRIBUTE (APP_CMD+1)
#define APP_DATA (APP_ATTRIBUTE+1)

/**
Suggested commands to be used in APP_CMD field in application payload.
*/
typedef enum
{
  APP_USER_INPUT,
  APP_SET_REQUEST,
  APP_GET_REQUEST,
  APP_SET_RESPONS,
  APP_GET_RESPONS,
  APP_SUSPEND
} command;

/**
Suggested payload APP_DATA contents for wireless mouse.   
*/
#define APP_MOUSE_BUTTONS APP_DATA
#define APP_MOUSE_X_DISP (APP_MOUSE_BUTTONS+1)
#define APP_MOUSE_Y_DISP (APP_MOUSE_X_DISP+1)
#define APP_MOUSE_Z_DISP (APP_MOUSE_Y_DISP+1)
#define APP_MOUSE_PL_LENGTH (APP_MOUSE_Z_DISP+1)

#if (APP_MOUSE_PL_LENGTH > WDP_MAX_UL_PL_LENGTH)
  #warning APP_MOUSE_PL_LENGTH must be >= WDP_MAX_UL_PL_LENGTH.  
#endif

/**
Suggested payload APP_DATA contents for wireless keyboard.
*/
#define APP_KEYB_MOD APP_DATA
#define APP_KEYB_KEYB_RES (APP_KEYB_MOD+1)
#define APP_KEYB_KEYS (APP_KEYB_KEYB_RES+1)
#define APP_KEYBOARD_PL_LENGTH (APP_KEYB_KEYS+6)

#if (APP_KEYBOARD_PL_LENGTH > WDP_MAX_UL_PL_LENGTH)
  #warning APP_KEYBOARD_PL_LENGTH must be >= WDP_MAX_UL_PL_LENGTH.  
#endif

/**
Suggested payload APP_DATA contents for remote control.  
*/
#define APP_REMOTE_DATA0 APP_DATA
#define APP_REMOTE_PL_LENGTH (APP_REMOTE_DATA0+1)

#if (APP_KEYBOARD_PL_LENGTH > WDP_MAX_UL_PL_LENGTH)
  #warning APP_REMOTE_PL_LENGTH must be >= WDP_MAX_UL_PL_LENGTH.  
#endif

/**
Function that must be called periodically by an auto reload timer.
The underlying frequency agility protocol (FAP) used by the WDP 
requires one auto reload timer which generates a periodically 
interrupt. This function should be called periodically by this
same interrupt service routine. The WDP uses this for necessary timing
when a continuous link is activated. 

Note that the body for this function differs between host and device.  
*/
void wdp_timer_isr_function(void);

/**
Constant for specifying a transmit timeout period of 5 ms.   
*/ 
#define WDP_TX_5MS (5000/FAP_MAX_TX_PERIOD)  

/**
Constant for specifying a transmit timeout period of 10 ms.   
*/ 
#define WDP_TX_10MS (10000/FAP_MAX_TX_PERIOD)  

/**
Constant for specifying a transmit timeout period of 20 ms.   
*/
#define WDP_TX_20MS (20000/FAP_MAX_TX_PERIOD)

/**
Constant for specifying a transmit timeout period of 30 ms.   
*/  
#define WDP_TX_30MS (30000/FAP_MAX_TX_PERIOD)

/**
Constant for specifying a transmit timeout period of 40 ms.   
*/  
#define WDP_TX_40MS (40000/FAP_MAX_TX_PERIOD)

/**
Constant for specifying a transmit timeout period of 50 ms.   
*/  
#define WDP_TX_50MS (50000/FAP_MAX_TX_PERIOD)

/**
Constant for specifying a transmit timeout period of 60 ms.   
*/  
#define WDP_TX_60MS (60000/FAP_MAX_TX_PERIOD)

/**
Constant for specifying a transmit timeout period of 70 ms.   
*/  
#define WDP_TX_70MS (70000/FAP_MAX_TX_PERIOD)  

/**
Constant for specifying a transmit timeout period of 80 ms.   
*/
#define WDP_TX_80MS (80000/FAP_MAX_TX_PERIOD)

/**
Constant for specifying a transmit timeout period of 90 ms.   
*/  
#define WDP_TX_90MS (90000/FAP_MAX_TX_PERIOD)

/**
Constant for specifying a transmit timeout period of 100 ms.   
*/  
#define WDP_TX_100MS (100000/FAP_MAX_TX_PERIOD)


/**
Constant for specifying a transmit timeout period of 5 ms.   
*/  
#define WDP_RX_5MS (5000/FAP_RX_PERIOD)

/**
Constant for specifying a receive timeout period of 100 ms.   
*/  
#define WDP_RX_100MS (100000/FAP_RX_PERIOD)

/**
Calculation of the channel bin size.   
*/  
#define WDP_CH_BIN_SIZE ((WDP_CH_HIGH - WDP_CH_LOW) /FAP_CH_TAB_SIZE)

/**
Function for generating an adequate subset of channels from a single 
 byte. 
*/
void wdp_extract_channels(uint8_t seed, uint8_t *ch_tab);

#if (WDP_MAX_UL_PL_LENGTH > WDP_MAX_DL_PL_LENGTH)
  #define WDP_MAX_PL_LENGTH WDP_MAX_UL_PL_LENGTH
#else
  #define WDP_MAX_PL_LENGTH WDP_MAX_DL_PL_LENGTH
#endif

#if(WDP_MAX_UL_PL_LENGTH > 31)
#error Constant WDP_MAX_UL_PL_LENGTH cannot exceed 31. The maximum \
radio payload length is 32, but 1 byte is required for protocol  \
purposes. 
#endif

#if(WDP_CH_HIGH < WDP_CH_LOW)
#error Constant WDP_CH_HIGH must be set higher than WDP_CH_LOW. 
#endif

#if( WDP_PAIRING_PL_LENGTH > FAP_MAX_FW_PL_LENGTH)
#error Constant WDP_MAX_UL_PL_LENGTH in file must be >= WDP_PAIRING_PL_LENGTH.
#endif 

#if( WDP_PAIRING_PL_LENGTH > FAP_MAX_ACK_PL_LENGTH )
#error Constant WDP_MAX_DL_PL_LENGTH must be >= WDP_PAIRING_PL_LENGTH.
#endif

#if( FAP_MAX_FW_PL_LENGTH < (WDP_APP_PL_START + WDP_MAX_UL_PL_LENGTH) )
#error Constant WDP_MAX_PL_LENGTH not in accordance with the definition \
of FAP_MAX_FW_PL_LENGTH. The following should be true: \
FAP_MAX_FW_PL_LENGTH >= WDP_APP_PL_START + WDP_MAX_UL_PL_LENGTH.
#endif

#if(FAP_MAX_ACK_PL_LENGTH != WDP_MAX_DL_PL_LENGTH)
#error FAP_MAX_ACK_PL_LENGTH must equal WDP_MAX_DL_PL_LENGTH.
#endif

#if(FAP_MAX_ACK_PL_LENGTH != WDP_MAX_DL_PL_LENGTH)
#error FAP_MAX_ACK_PL_LENGTH must equal WDP_MAX_DL_PL_LENGTH.
#endif

#endif
/** @} */
    
